<template>
    <div id="qqlogin">
        <div v-for="(item, index) in logs" :key="index">
            {{ item.time }} --- {{ item.msg }}
        </div>
    </div>
</template>
<script>
import { qqLogin } from '@/api/api.js'
import isPE from '@/utils/isPE'
export default {
    data() {
        return {
            logs: []
        }
    },
    created() {
        this.log("isPE=" + isPE())
        this.log("created")
        QC.Login.getMe((openId, accessToken) => {
            console.log(openId, accessToken)
            this.log("openId=" + openId)
            this.log("accessToken=" + accessToken)
            qqLogin(openId, accessToken).then(res => {
                res = res.data
                this.log("res.code=" + res.code)
                if (res.code == 200) {
                    this.log("token=" + res.data.token)
                    if (isPE()) {
                        let peRedirectPath = this.$route.query.peRedirectPath
                        //pe登录，获取不到window.opener，就调用这个地址，把token传过去
                        let peHandleQQLoginPage = this.$route.query.peHandleQQLoginPage
                        this.log("peRedirectPath=" + peRedirectPath)
                        this.log("peHandleQQLoginPage=" + peHandleQQLoginPage)

                        let arr = []
                        arr.push("peRedirectPath=" + encodeURI(peRedirectPath))
                        arr.push("token=" + res.data.token)
                        if (token) {
                            window.location.href = decodeURI(peHandleQQLoginPage) + "?" + arr.join("&")
                        }
                    } else {
                        if (window.opener) {
                            let callback = this.$route.query.callback
                            if (callback) {
                                let obj = {}
                                obj[callback] = res.data
                                window.opener.postMessage(obj, '*')
                                this.log("完成，关闭页面")
                                this.$nextTick(() => {
                                    window.close()
                                })
                            } else {
                                this.log("无callback")
                            }
                        } else {
                            this.log("无window.opener")
                        }
                    }
                } else {
                    this.log("data=" + res.data)
                    this.log("msg=" + res.msg)
                }
            })
        })
    },
    methods: {
        log(msg) {
            this.logs.push({
                time: new Date().toLocaleString(),
                msg
            })
        }
    }
}
</script>